#include "AppDelegate.h"
#include "SimpleAudioEngine.h"
#include "HelloWorldScene.h"
#include "MyEGLView.h"

#include "script_support/CCScriptSupport.h"
#include "CCLuaEngine.h"

USING_NS_CC;
using namespace CocosDenshion;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate() 
{
	SimpleAudioEngine::sharedEngine()->end();
}

int AppDelegate::run()
{
	//PVRFrameEnableControlWindow(false);

	// Main message loop:
//	MSG msg;
	LARGE_INTEGER nFreq;
	LARGE_INTEGER nLast;
// 	LARGE_INTEGER nNow;

	QueryPerformanceFrequency(&nFreq);
	QueryPerformanceCounter(&nLast);

	// Initialize instance and cocos2d.
	if (!applicationDidFinishLaunching())
	{
		return 0;
	}

// 	while (1)
// 	{
// 		if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
// 		{
// 			// Get current time tick.
// 			QueryPerformanceCounter(&nNow);
// 
// 			// If it's the time to draw next frame, draw it, else sleep a while.
// 			if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)
// 			{
// 				nLast.QuadPart = nNow.QuadPart;
// 				CCDirector::sharedDirector()->mainLoop();
// 			}
// 			else
// 			{
// 				Sleep(0);
// 			}
// 			continue;
// 		}
// 
// 		if (WM_QUIT == msg.message)
// 		{
// 			// Quit message loop.
// 			break;
// 		}
// 
// 		// Deal with windows message.
// 		if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))
// 		{
// 			TranslateMessage(&msg);
// 			DispatchMessage(&msg);
// 		}
// 	}
// 
// 	return (int) msg.wParam;
	return 1;
}

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
	CCEGLView *pEGLView = (CCEGLView*)MyEGLView::sharedOpenGLView();

    pDirector->setOpenGLView(pEGLView);

    // turn on display FPS
    //pDirector->setDisplayStats(true);

	
    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);

	/*
	CCEGLView::sharedOpenGLView()->setDesignResolutionSize(480, 320, kResolutionShowAll);
	CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
	CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);

	std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("hello.lua");
	pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
	pEngine->executeScriptFile(path.c_str());
	*/

	//CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd");
	MyEGLView::sharedOpenGLView()->setDesignResolutionSize(960, 640, kResolutionNoBorder);
	CCScene *scene = HelloWorld::scene();
	pDirector->runWithScene(scene);

    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    CCDirector::sharedDirector()->stopAnimation();

    // if you use SimpleAudioEngine, it must be pause
    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    CCDirector::sharedDirector()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
